package com.china08.yxyapi.api.manage.sch.platform.website.template;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSchoolTemplet;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.entity.db.sws.SwsTempletModule;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsModuleRepository;
import com.china08.yxyapi.repository.db.SwsSchoolTempletRepository;
import com.china08.yxyapi.repository.db.SwsSettingRepository;
import com.china08.yxyapi.repository.db.SwsSettingTempletRepository;
import com.china08.yxyapi.repository.db.SwsTempletModuleRepository;

@CrossOrigin
@RestController
public class SwsTemplateUpdDefaultController extends YxyApiBaseController {

    @Autowired
    SwsTempletModuleRepository swsTempletModuleRepository;

    @Autowired
    SwsSettingTempletRepository swsSettingTempletRepository;

    @Autowired
    SwsSettingRepository swsSettingRepository;

    @Autowired
    SwsSchoolTempletRepository swsSchoolTempletRepository;

    @Autowired
    SwsModuleRepository SwsModuleRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/template/upd/default", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {
        String templId = reqModel.templId;
        Assert.isTrue(StringUtils.isNotBlank(templId), "模版为空");

        String schoolId = getProfile(request, response).getSchoolId();

        // 保存学校配置信息
        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);
        Assert.isTrue(null != swsSetting, "");
        swsSetting.setSkinTempl(templId);
        swsSetting.setSkinTheme("theme001");
        swsSetting.setSkinColor("color001");
        swsSettingRepository.save(swsSetting);

        // 模版对应的所有模块
        List<SwsTempletModule> templList = swsTempletModuleRepository.findByTemplateId(templId);

        // 删除学校所有配置模块
        List<SwsSchoolTemplet> schoolTempletsList = swsSchoolTempletRepository.findBySchoolId(schoolId);
        swsSchoolTempletRepository.delete(schoolTempletsList);

        // 从新添加
        for (SwsTempletModule swsTempletModule : templList) {
            Integer orderNum = templList.indexOf(swsTempletModule) + 1;
            SwsSchoolTemplet swsSchoolTemplet = new SwsSchoolTemplet();
            swsSchoolTemplet.setId(UUID.randomUUID().toString().replace("-", ""));
            swsSchoolTemplet.setSchoolId(schoolId);
            swsSchoolTemplet.setTemplateId(templId);
            swsSchoolTemplet.setOrderNum(orderNum);
            swsSchoolTemplet.setModuleId(swsTempletModule.getModuleId());
            swsSchoolTempletRepository.save(swsSchoolTemplet);
        }

        return null;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String templId;

    }

    protected class RespModel extends YxyApiRespModel {

    }
}
